<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        人员添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/css/main.css" media="all">
    <script type="text/javascript" src="/lib/loading/okLoading.js"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>业主信息管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="personal" :rules="rules" ref="personalFormRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="所属小区" prop="communityName">
                <el-select v-model="communityVo" placeholder="请选择所属小区" value-key="id">
                    <el-option
                            v-for="community in communityList"
                            :key="community.id"
                            :label="community.name"
                            :value="community">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="所属房产" prop="homeName">
                <el-select v-model="homeVo" placeholder="请选择所属房产" value-key="id">
                    <el-option
                            v-for="home in homeList"
                            :key="home.id"
                            :label="home.name"
                            :value="home">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="成员名称" prop="name">
                <el-input v-model="personal.name"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="idCard">
                <el-input v-model="personal.idCard"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="telephone">
                <el-input v-model="personal.telephone"></el-input>
            </el-form-item>
            <el-form-item label="职业" prop="profession">
                <el-input v-model="personal.profession"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="personal.sex">
                    <el-radio label="0">男</el-radio>
                    <el-radio label="1">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="成员类型" prop="type">
                <el-radio-group v-model="personal.type">
                    <el-radio label="0">房主</el-radio>
                    <el-radio label="1">租客</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="personal.remark" type="textarea">
                </el-input>
            </el-form-item>
            <el-form-item label="出生日期">
                <el-date-picker
                        v-model="personal.birthday"
                        type="datetime"
                        placeholder="选择入住时间"
                        prop="liveTime"
                        align="right"
                        :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="成员照片" prop="picture">
                <el-upload
                        class="upload-demo"
                        action="/estate/fileupload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleSuccess"
                        :file-list="fileList"
                        list-type="picture">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10M</div>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script src="js/vue.js" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script src="js/index.js"></script>
<!--导入Axios依赖包-->
<script src="js/axios.js" charset="utf-8"></script>
<script type="text/javascript">
    let checkPhone;
    checkPhone = (rule, value, callback) => {
        const phoneReg = /^1[3|4|5|6|7|8][0-9]{9}$/
        if (!value) {
            return callback(new Error('电话号码不能为空'))
        }
        setTimeout(() => {

            if (!Number.isInteger(+value)) {
                callback(new Error('请输入数字值'))
            } else {
                if (phoneReg.test(value)) {
                    callback()
                } else {
                    callback(new Error('电话号码格式不正确'))
                }
            }
        }, 100)
    }
    var app = new Vue({
        el:"#app",
        data:{
            title:"添加成员",
            personal:{picture:"",id:""},
            fileList:[],
            communityVo:{},
            homeVo:{},
            communityList:{},
            homeList:{},
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            rules: {
                communityName: [
                    { required: true, message: '请选择小区名称', trigger: 'blur' },
                ],
                homeName: [
                    { required: true, message: '请选择所属房产', trigger: 'blur' },
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min:2,max: 20, message: '长度必须大于2、小于20个字符', trigger: 'blur' }
                ],
                idCard: [
                    { required: true, message: '请输入身份证号', trigger: 'blur' }
                ],
                telephone: [
                    { required: true, message: '请输入联系方式', trigger: 'blur' },
                    { validator: checkPhone, message: '联系方式格式不正确', trigger: 'blur' }
                ],
                profession: [
                    { required: true, message: '请输入职业', trigger: 'blur' }
                ],
                greeningRate: [
                    { required: true, message: '请输入绿化面积', trigger: 'blur' },
                ],
                totalBuildings:[
                    { required: true, message: '请输入楼栋总数', trigger: 'blur' }
                ],
                totalHouseholds:[
                    { required: true, message: '请输入住户总数', trigger: 'blur' }
                ],
                sex:[
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                type:[
                    { required: true, message: '请选择人员类型', trigger: 'change' }
                ],
                picture:[
                    { required: true, message: '请上传缩略图', trigger: 'change' }
                ]
            }
        },
        methods:{
            findById(){
                axios.get("/personal/findById?id="+this.personal.id).then((res)=>{
                    this.personal = res.data.data;
                    this.fileList.push({url:"http://localhost/estate"+this.personal.picture});
                });
            },
            findCommunity(){
                axios.get("/community/find").then((res)=>{
                    this.communityList = res.data.data;
                });
            },
            findHome(){
                axios.get("/home/find").then((res)=>{
                    this.homeList = res.data.data;
                });
            },
            handleSuccess(response, file, fileList){
                this.personal.picture=response.data;
            },
            handleRemove() {
                axios.get("/estate/delfile?fileName="+this.personal.picture).then((res)=>{
                    app.personal.picture="";
                    if(res.data.flag){
                        app.fileList=[];
                        this.$message({
                            message: '图片删除成功',
                            type: 'success'
                        });
                    }
                });
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            //文件删除提示
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            //表达重置
            clearAll() {
                this.$refs.personalFormRef.resetFields();
                if(this.fileList.length>0) {
                    this.handleRemove();
                }
            },
            save(){
                var method = "";
                if(this.personal.id != null){
                    method = "update"
                }else{
                    method = "add"
                }
                this.personal.communityName=this.communityVo.name;
                this.personal.houseName=this.homeVo.name;
                axios.post("/personal/"+method,this.personal).then((res)=>{
                    if(res.data.flag){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            imgUrl(){
                return "http://localhost/"+app.personal.picture;
            },
            getQueryString(name){
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        },
        created:function(){
            layui.use(['element','layer','form'], function(){
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.personal.id = this.getQueryString("id");
            //根据小区ID获取小区对象，进行数据回显
            if(this.personal.id != null && this.personal.id !== ''){
                this.title="修改成员";
                this.findById();
            }
            this.findCommunity();
            this.findHome();
        }
    });
</script>
</body>
</html>